package com.zj.sixtyDayChallenge.array;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 移除元素
 * 27 remove-element
 * @author: Zhang Bo
 * @create: 2022-12-19 17:11:41
 **/
public class RemoveElement {
    public static void main(String[] args) {
        Solution solution = new RemoveElement().new Solution();
        int[] arr = {0, 1, 2, 2, 3, 0, 4, 2};
        System.out.println(solution.removeElement(arr, 2));
        System.out.println(Arrays.toString(arr));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        /**
         * 1 3 4 5 2 3 6 2 1
         *
         * @param nums
         * @param val
         * @return
         */
        public int removeElement(int[] nums, int val) {

            int i = 0;
            int j = 0;
            while (i < nums.length) {

                if (i > j) {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }

                if (val != nums[j]) {
                    j++;
                }
                i++;
            }

            return j;

        }

        public int removeElement01(int[] nums, int val) {

            int fast = 0;
            int slow = 0;
            while (fast < nums.length) {

                if (fast != slow) {
                    int temp = nums[fast];
                    nums[fast] = nums[slow];
                    nums[slow] = temp;
                }
                if (val != nums[slow]) {
                    slow++;
                }
                fast++;

            }

            return slow;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}